package com.example.projects;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;

import com.facebook.android.Facebook;



public class Information extends CPT3004Menu {
        final public static String BARCODE = "com.example.projects.MESSAGE";
        private ViewPager _mViewPager;
        private InfoViewPagerAdapter _adapter;
        private Facebook fb;
        private SharedPreferences mPrefs;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.information);
        setUpView();
        setTab();
        
        // set up facebook
        fb = ((CPT3004)getApplication()).getFacebook();
		if(fb == null) {
    		// create new facebook
    		fb = new Facebook(((CPT3004)getApplication()).getAppID());
    		
    		// prepare facebook for use
    		mPrefs = getSharedPreferences("", MODE_PRIVATE);
        	String access_token = mPrefs.getString("access_token", null);
        	long expires = mPrefs.getLong("access_expires", 0);
        	
        	// reuse old token if possible
        	if (access_token != null) {
    	    	fb.setAccessToken(access_token);
	        	if(expires != 0) {
	        		fb.setAccessExpires(expires);
	        	}
	        	fb.extendAccessTokenIfNeeded(this, null);
        	}
    		// store facebook
    		((CPT3004)getApplication()).setFacebook(fb);
    	}
		
        
    }
    
        @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK) { 
                Intent intent = getIntent();
                String barcode = intent.getStringExtra(Main.BARCODE);
                Intent intent2 = new Intent(this, Main.class);
                //direct the barcode to comment class
                intent2.putExtra(Main.BARCODE, barcode);
                intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent2);
        }
        return super.onKeyDown(keyCode, event); 
        }
    
    private void setUpView(){           
        _mViewPager = (ViewPager) findViewById(R.id.infoviewPager);
        _adapter = new InfoViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
        _mViewPager.setAdapter(_adapter);
                _mViewPager.setCurrentItem(0);
    }
    private void setTab(){
        _mViewPager.setOnPageChangeListener(new OnPageChangeListener(){
                                        
                        public void onPageScrollStateChanged(int position) {}
                        public void onPageScrolled(int arg0, float arg1, int arg2) {}
                        public void onPageSelected(int position) {
                                // TODO Auto-generated method stub
                                switch(position){
                                case 0:
                                        findViewById(R.id.first_infotab).setVisibility(View.VISIBLE);
                                        findViewById(R.id.second_infotab).setVisibility(View.INVISIBLE);
                                        break;
                                        
                                case 1:
                                        findViewById(R.id.first_infotab).setVisibility(View.INVISIBLE);
                                        findViewById(R.id.second_infotab).setVisibility(View.VISIBLE);
                                        break;
                                }
                        }
                        
                });

    }
    
    @Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
			fb.authorizeCallback(requestCode, resultCode, data);
	}
}
